import { login, logout } from '@/api/user'

// 用户相关数据管理
export default {
  state: {
    username: '',
    token: '',
    isLogin: false
  },
  mutations: {
    login(state, payload) {
      state.username = payload.username
      state.taken = payload.taken
      state.isLogin = true
    },
    logout(state) {
      state.username = ''
      state.taken = ''
      state.isLogin = false
    }
  },
  actions: {
    // 异步登录
    loginAsync(context, payload) {
      const { username, password } = payload
      // 调用api login函数
      return login(username, password).then(res => {
        context.commit('login', res)
        return {
          isLogin: true
        }
      }).catch(e => console.log(e))
    },
    logoutAsync(context) {
      return logout().then(res => {
        context.commit('logout')
        return res
      })
    }
  },
  getters: {}
}
